<template>
  <a-modal
    :title="(model && model.id) ? '编辑' : '新增'"
    :width="900"
    :height="360"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleOK"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" style="display: none;">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="审核状态">
          <a-radio-group name="radioGroup" :default-value="1" v-decorator="['auditState', { rules: [{ required: true, message: '请选择审核状态' }] }]">
            <a-radio :value="0">
              撤销
            </a-radio>
            <a-radio :value="1">
              通过
            </a-radio>
            <a-radio :value="2">
              驳回
            </a-radio>
            <a-radio :value="-1">
              已撤销
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="审核备注">
          <a-textarea v-decorator="['auditNote', { rules: [{ required: false, message: '请输入审核备注' }] }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
// 表单字段
const fields = ['id', 'radioGroup', 'auditNote']
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      if (this.model) {
        this.form.setFieldsValue(pick(this.model, fields))
      }
    })
  },
  methods: {
    handleOK () {
      this.$emit('ok')
    }
  }
}
</script>
<style scoped>
.avatar-uploader>.ant-upload {
  width: 128px;
  height: 128px;
}

.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
